package com.dailygreetings.dailygreetings.utils;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
/**
 * @BelongsProject: daily-greetings
 * @BelongsPackage: com.dailygreetings.dailygreetings.utils
 * @Author: shanzhengfei
 * @CreateTime: 2024-11-21  18:12
 * @Description: 处理日期相关工具类
 * @Version: 1.0
 */

public class DateUtils {

    public static String getToday() {
        LocalDate today = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd EEEE");
        return today.format(formatter);
    }

    public static String getDay() {
        LocalDate today = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return today.format(formatter);
    }

    public static long getDaysUntilBirthday(LocalDate birthday) {
        LocalDate today = LocalDate.now();
        LocalDate nextBirthday = birthday.withYear(today.getYear());
        if (nextBirthday.isBefore(today) || nextBirthday.isEqual(today)) {
            nextBirthday = nextBirthday.plusYears(1);
        }
        return ChronoUnit.DAYS.between(today, nextBirthday);
    }

    public static String getBabyAge(LocalDate babyDays) {
        LocalDate today = LocalDate.now();
        Period period = Period.between(babyDays, today);
        return period.getYears() + " 年 " + period.getMonths() + " 个月 " + period.getDays() + " 天";
    }

    public static long getDaysUntilAnniversary(String anniversary) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd");
        LocalDate today = LocalDate.now();
        LocalDate nextAnniversary = LocalDate.parse(anniversary + "-" + today.getYear(), DateTimeFormatter.ofPattern("MM-dd-yyyy"));
        if (nextAnniversary.isBefore(today) || nextAnniversary.isEqual(today)) {
            nextAnniversary = nextAnniversary.plusYears(1);
        }
        return ChronoUnit.DAYS.between(today, nextAnniversary);
    }
}
